import { getProdectList } from "@Api/productApi"
import { Table, Button, Space, ImgDefault, Icon, message, Modal, Form, Radio, Checkbox, InputNumber, Spin, Select, Progress } from '@bytedance/mona-ui';

// import moment from 'moment';
let addInfoLimit = 0
let getPredectListall = []
let errpage = []
let cletaerDtae
let addLimitStop = 50
let repeadter = 0
let allPages = 0
export function getProductAllApi(shearDater, cobs) {
    errpage = []
    cletaerDtae = null
    addInfoLimit = 0
    addLimitStop = 50
    repeadter = 0
    allPages = 0
    return new Promise((resolve, reject) => {
        getProdectList(shearDater).then((res) => {
            // if (res.code) {
            //     message.error(res.msg)
            //     setGoodsLoading(false)
            //     return
            // }
            console.log('res', res)
            if (res.code) {
                message.warning(res.msg)
                return
            }
            getPredectListall = forMartQuerData(res.data) || []
            allPages = Math.ceil(res.total / 20) - 1
            if (res.total <= 700) {
                addLimitStop = 70
                // addLimitStop = 70
            }
            // goodsDataInfoCont.total = res.total
            // setGoodsDaraInfoCont({ ...goodsDataInfoCont })
            shearDater.page = 0
            getPredectMiter(res.total, shearDater, (data) => {

                resolve(data)
            }, cobs)
        })
    })

}


function getPredectMiter(total, shearDater, cb, cobs) {
    addInfoLimit = 0
    // 是否加载全部
    const getallData = []

    while ((shearDater.pageSize * (shearDater.page + 1)) < total && addInfoLimit < addLimitStop) {
        // console.log('chetraer', ((shearDater.pageSize * (shearDater.page + 1)) < total) && (addInfoLimit < addLimitStop))
        addInfoLimit += 1
        shearDater.page++
        // console.log('addNums', addInfoLimit, addLimitStop)
        getallData.push(getProdectList(shearDater).then(res => {
            repeadter++
            // console.log('统计进度', ((repeadter / allPages) * 100).toFixed(2) + '%')
            cobs && cobs(((repeadter / allPages) * 100).toFixed(2))
            Promise.resolve(res)
            return res
        }))
    }
    // console.log('getallData', getallData)

    Promise.all(getallData).then(resit => {
        // console.log('resit', resit)
        resit.forEach(resdata => {
            if (!resdata.code) {
                getPredectListall.push(...forMartQuerData(resdata.data))
            } else {
                errpage.push({
                    config: resdata.config
                })
            }
        })

        if ((shearDater.pageSize * (shearDater.page + 1)) < total) {
            cletaerDtae = setTimeout(_ => {
                // console.log('继续获取', getPredectListall)
                getPredectMiter(total, shearDater, cb, cobs)
            }, 5000)
            return
        } else {
            // console.log('获取完成', getPredectListall)
            // const addProductList = {
            //     create_time: Date.now(),
            //     list: getPredectListall
            // }
            // localStorage.setItem('infoProdectList', JSON.stringify(addProductList))
            cb && cb(getPredectListall)
        }
    })
}
function forMartQuerData(data) {
    return data?.map(resaqer => {
        return {
            img: resaqer.img,
            name: resaqer.name,
            product_id: resaqer.product_id,
            sell_num: resaqer.sell_num,
            stock_num: resaqer.stock_num,
            create_time: resaqer.create_time,
            update_time: resaqer.update_time,
            // 创建时间到今天的时间天数

            day: ((Date.now() - new Date(resaqer.create_time).getTime()) / (1000 * 60 * 60 * 24)).toFixed(0),

            price: (() => {
                const { price_lower, price_higher } = resaqer
                return price_lower === price_higher ? (price_lower / 100).toFixed(2) : `${(price_lower / 100).toFixed(2)}~${(price_higher / 100).toFixed(2)}`

            })(),
        }
    })
}